package com.axxc.jpa.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.omg.CORBA.SystemException;
import org.omg.CORBA.portable.InputStream;
import org.omg.CORBA.portable.InvokeHandler;
import org.omg.CORBA.portable.OutputStream;
import org.omg.CORBA.portable.ResponseHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.axxc.jpa.entity.HumanEntity;
import com.axxc.jpa.out.filter.HumanFilterHasDelete;
import com.axxc.jpa.service.IHumanService;
import com.axxc.jpa.vo.HumanVO;
import com.axxc.jpa.vo.ResponseVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @ClassName: PersonController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author 熊小川
 * @date 2018年5月12日 上午10:28:50
 */
@RequestMapping("/person")
@RestController
@Api(description = "用户接口文档")
public class HumanController extends AbstractCurdController<HumanVO, HumanVO, Long> {

	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private IHumanService personService;

	@ApiOperation(value = "根据条件查询用户")
	@GetMapping("findPersonByPredicate")
	public ResponseVO<List<HumanEntity>> findPersonByPredicate(@ModelAttribute HumanVO HumanVO) {

		Pageable pageable = new PageRequest(HumanVO.getPageNum() - 1, HumanVO.getPageSize());
		HumanFilterHasDelete personFilter = new HumanFilterHasDelete(HumanVO);
		logger.info("HumanFilter:" + JSONObject.toJSONString(personFilter));
		Page<HumanEntity> personPage = this.personService.listPersonAndPage(personFilter, pageable);

		ResponseVO<List<HumanEntity>> responseVO = new ResponseVO<>(personPage.getContent());
		if (personPage != null) {
			responseVO.setMessage("success");
			responseVO.setStatus(HttpStatus.OK);
			responseVO.setContent(personPage.getContent());
			responseVO.setTotal(personPage.getTotalElements());
			responseVO.setTotalPage(personPage.getTotalPages());
			responseVO.setCurrentPage(personPage.getNumber() + 1);
			responseVO.setPageSize(personPage.getSize());
		}
		return responseVO;

	}

	@ApiOperation(value = "根据id查询用户")
	@GetMapping("findOne")
	public ResponseVO<HumanEntity> findOne(@RequestParam(defaultValue = "1") long id) {
		return new ResponseVO<>(this.personService.findOne(id));

	}

	@SuppressWarnings("serial")
	@ApiOperation(value = "java反模式初始化集合")
	@GetMapping("lists")
	public ResponseVO<List<Object>> lists() {

		//外部大括号声明一个匿名内部类,
		//内部括号 为代码块,利用实体初始化的特性,故此为反模式
		List<Object> lists = new ArrayList<Object>() {{
				add("A");
				add("B");
				add(new HashMap<String, Object>(){{
					put("CC", "CC");
					put("DD", "DD");
					put("EE", new HashMap<String, Object>(){{
						put("FF", "FF");
					}});
				}});
		}};

		return new ResponseVO<>(lists);

	}

}
